home *** CD-ROM | disk | FTP | other *** search
Wrap
package eval; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.List; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; public class Eval extends MIDlet implements CommandListener { public static final int VERSION = 1026; public static final int MAX_LEN = 512; public static int PRECISION = 10; public static int HISTORY_SIZE = 10; private static final String ABOUT = "Supported operators are: + - / * ( ) =, functions: sin(), cos(), constants: Pi.\nTo store result in a variable use '=' operator, for example: var1=sin(pi/4)*sin(pi/4)+cos(pi/4)*cos(pi/4)\nMidlet developed by WAP INDUSTRIAL [support@wapindustrial.com]. We can develop customized midlets for you!"; private static final String ITEM0 = "\n=press the SELECT button="; private Display display; private List hist; private TextBox entry; private TextBox prec; private Alert directions = new Alert("Help"); private int histIndex = 0; private Command exitCommand = new Command("Exit", 7, 4); private Command cancelEditCommand = new Command("Back", 2, 2); private Command okEditCommand = new Command("OK", 4, 1); private Command aboutCommand = new Command("Help", 5, 3); private Command precisionHistCommand = new Command("Precision", 1, 5); private Command cancelPrecCommand = new Command("Back", 2, 2); private Command okPrecCommand = new Command("OK", 4, 1); public Eval() { this.directions.setTimeout(-2); this.directions.setString("Eval v" + getVersion() + " " + "Supported operators are: + - / * ( ) =, functions: sin(), cos(), constants: Pi.\nTo store result in a variable use '=' operator, for example: var1=sin(pi/4)*sin(pi/4)+cos(pi/4)*cos(pi/4)\nMidlet developed by WAP INDUSTRIAL [support@wapindustrial.com]. We can develop customized midlets for you!"); } private void about() { Display.getDisplay(this).setCurrent(this.directions); } public void commandAction(Command var1, Displayable var2) { if (var1 == List.SELECT_COMMAND) { this.histIndex = this.hist.getSelectedIndex(); String var3 = this.hist.getString(this.histIndex); int var4 = var3.indexOf("\n="); if (var4 >= 0) { var3 = var3.substring(0, var4); } this.entry.setString(var3); this.display.setCurrent(this.entry); } else if (var1 == this.precisionHistCommand) { this.prec.setString(Integer.toString(MyDecimal.getPrecision())); this.display.setCurrent(this.prec); } else if (var1 == this.cancelPrecCommand) { this.display.setCurrent(this.hist); } else if (var1 == this.okPrecCommand) { int var8 = 0; try { var8 = Integer.parseInt(this.prec.getString()); } catch (NumberFormatException var7) { } if (var8 > 0) { MyDecimal.setPrecision(var8); } this.display.setCurrent(this.hist); } else if (var1 != this.okEditCommand && var1 != this.cancelEditCommand) { if (var1 == this.exitCommand) { this.destroyApp(false); ((MIDlet)this).notifyDestroyed(); } else if (var1 == this.aboutCommand) { this.about(); } } else { if (var1 == this.okEditCommand) { String var9 = this.entry.getString(); String var11 = this.hist.getString(this.histIndex); if (var11.compareTo("\n=press the SELECT button=") == 0) { this.hist.delete(this.histIndex); } try { Result var5 = Result.Evaluate(var9); var9 = var9 + "\n=" + var5.toString(); } catch (BadFormulaException var6) { var9 = var9 + "\n=" + ((Throwable)var6).getMessage(); } if (var9.compareTo(var11) != 0) { this.hist.insert(0, var9, (Image)null); if (this.hist.size() > HISTORY_SIZE) { this.hist.delete(HISTORY_SIZE - 1); } } } this.hist.setSelectedIndex(this.histIndex, true); this.display.setCurrent(this.hist); } } public void destroyApp(boolean var1) { this.pauseApp(); } private int getInt(byte[] var1, int var2) { return (var1[var2] & 255) << 24 | (var1[var2 + 1] & 255) << 16 | (var1[var2 + 2] & 255) << 8 | var1[var2 + 3] & 255; } public static String getVersion() { String var0 = Integer.toString(0); var0 = var0 + "." + Integer.toString(4); var0 = var0 + "." + Integer.toString(2); return var0; } public void pauseApp() { this.writeProfile(); this.display.setCurrent((Displayable)null); this.entry = null; this.hist = null; } private void putInt(byte[] var1, int var2, int var3) { var1[var2] = (byte)(var3 >> 24 & 255); var1[var2 + 1] = (byte)(var3 >> 16 & 255); var1[var2 + 2] = (byte)(var3 >> 8 & 255); var1[var2 + 3] = (byte)(var3 & 255); } private void readProfile() { RecordStore var1 = null; byte[] var2 = new byte[512]; try { var1 = RecordStore.openRecordStore("eval", true); int var3 = var1.getRecord(1, var2, 0); if (var3 != 16) { throw new Exception(); } if (this.getInt(var2, 0) != 1026) { throw new Exception(); } MyDecimal.setPrecision(this.getInt(var2, 4)); int var4 = this.getInt(var2, 8); int var5 = this.getInt(var2, 12); int var6 = 2; for(int var7 = 0; var7 < var4; ++var7) { int var8 = var1.getRecord(var6++, var2, 0); String var9 = new String(var2, 0, var8); this.hist.append(var9, (Image)null); } for(int var15 = 0; var15 < var5; ++var15) { int var16 = var1.getRecord(var6++, var2, 0); int var10 = this.getInt(var2, 0); String var11 = new String(var2, 4, var10); var16 -= 4 + var10; String var12 = new String(var2, 4 + var10, var16); Result.addVar(new Variable(var11, new Result(new MyDecimal(var12)), false)); } } catch (Exception var14) { } if (var1 != null) { try { var1.closeRecordStore(); } catch (Exception var13) { } } if (this.hist.size() <= 0) { this.hist.append("\n=press the SELECT button=", (Image)null); } } public void startApp() { MyDecimal.setPrecision(PRECISION); this.display = Display.getDisplay(this); this.entry = new TextBox("Type Expression", (String)null, 512, 0); this.prec = new TextBox("digits after point", (String)null, 2, 2); this.hist = new List("Select Expession", 3); Result.addVar(new Variable("pi", new Result(MyDecimal.PI()), true)); this.readProfile(); this.hist.addCommand(this.exitCommand); this.hist.addCommand(this.aboutCommand); this.hist.addCommand(this.precisionHistCommand); this.hist.setCommandListener(this); this.entry.addCommand(this.cancelEditCommand); this.entry.addCommand(this.okEditCommand); this.entry.setCommandListener(this); this.prec.addCommand(this.cancelPrecCommand); this.prec.addCommand(this.okPrecCommand); this.prec.setCommandListener(this); this.display.setCurrent(this.hist); } private void writeProfile() { try { int var2 = 0; int var3 = Result.numberOfVars(false); RecordStore var1 = RecordStore.openRecordStore("eval", true); byte[] var4 = new byte[512]; this.putInt(var4, 0, 1026); this.putInt(var4, 4, MyDecimal.getPrecision()); this.putInt(var4, 8, this.hist.size()); this.putInt(var4, 12, var3); try { var1.setRecord(1, var4, 0, 16); } catch (RecordStoreException var12) { var1.addRecord(var4, 0, 16); } ++var2; for(int var5 = 0; var5 < this.hist.size(); ++var5) { String var6 = this.hist.getString(var5); byte[] var7 = var6.getBytes(); try { var1.setRecord(var2 + 1, var7, 0, var6.length()); } catch (RecordStoreException var11) { var1.addRecord(var7, 0, var6.length()); } ++var2; } for(int var15 = 0; var15 < Result.vars.size(); ++var15) { int var16 = 0; Variable var8 = (Variable)Result.vars.elementAt(var15); if (!var8.persistent) { this.putInt(var4, 0, var8.name.length()); var16 += 4; System.arraycopy(var8.name.getBytes(), 0, var4, 4, var8.name.length()); var16 += var8.name.length(); String var9 = var8.val.toString(); System.arraycopy(var9.getBytes(), 0, var4, var16, var9.length()); var16 += var9.length(); try { var1.setRecord(var2 + 1, var4, 0, var16); } catch (RecordStoreException var10) { var1.addRecord(var4, 0, var16); } ++var2; } } } catch (RecordStoreException var13) { ((Throwable)var13).printStackTrace(); } } }